package com.itheima.service.store.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.domain.store.Question;
import com.itheima.factory.MapperFactory;
import com.itheima.mapper.store.QuestionMapper;
import com.itheima.service.store.QuestionService;
import com.itheima.utils.TransactionUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;


import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 部门 业务层 实现
 */
public class QuestionServiceImpl implements QuestionService {
    @Override
    public Page<Question> findAll(String pageNum, String pageSize) {
        //获取sqlSession对象
        SqlSession sqlSession = MapperFactory.getSqlSession();
        //获取映射对象
        QuestionMapper mapper = MapperFactory.getMapper(sqlSession, QuestionMapper.class);
        //配置分页信息
        int num = 1;
        int size = 10;
        //阿帕奇的StringUtils.isNotBlank() 可以同时判断空, 无值, 空格
        if (StringUtils.isNotBlank(pageNum)) {
            num = Integer.parseInt(pageNum);
        }
        if (StringUtils.isNotBlank(pageSize)) {
            size = Integer.parseInt(pageSize);
        }
        Page<Question> page = PageHelper.startPage(num, size);
        //查找全部
        mapper.findAll();
        //释放资源
        TransactionUtil.close(sqlSession);

        return page;
    }

    @Override
    public List<Question> findAll() {
        //获取sqlSession对象
        SqlSession sqlSession = MapperFactory.getSqlSession();
        //获取映射对象
        QuestionMapper mapper = MapperFactory.getMapper(sqlSession, QuestionMapper.class);
        //查找全部
        Page<Question> list = mapper.findAll();
        //释放资源
        TransactionUtil.close(sqlSession);

        return list;
    }

    @Override
    public void addQuestion(Question question) {
        //获取sqlSession对象
        SqlSession sqlSession = MapperFactory.getSqlSession();
        //获取映射对象
        QuestionMapper mapper = MapperFactory.getMapper(sqlSession, QuestionMapper.class);
        //查找全部
        mapper.addQuestion(question);
        //提交事务
        TransactionUtil.commit(sqlSession);
        //释放资源
        TransactionUtil.close(sqlSession);

    }

    @Override
    public boolean deleteByIds(String ids) {
        try {
            SqlSession sqlSession = MapperFactory.getSqlSession();
            //获取映射对象
            QuestionMapper mapper = MapperFactory.getMapper(sqlSession, QuestionMapper.class);
            //ids处理
            String[] idArr = ids.split(",");
            mapper.deleteByIds(idArr);
            //提交事务
            TransactionUtil.commit(sqlSession);
            //释放资源
            TransactionUtil.close(sqlSession);

        } catch (Exception e) {
            return false;
        }

        return true;
    }

    @Override
    public Question findById(String id) {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        //获取映射对象
        QuestionMapper mapper = MapperFactory.getMapper(sqlSession, QuestionMapper.class);
        //查找用户
        Question question = mapper.findById(id);
        //释放资源
        TransactionUtil.close(sqlSession);
        return question;
    }

    @Override
    public void updateQuestion(Question question) {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        //获取映射对象
        QuestionMapper mapper = MapperFactory.getMapper(sqlSession, QuestionMapper.class);
        //更新信息
        mapper.updateQuestion(question);
        //提交事务
        TransactionUtil.commit(sqlSession);
        //释放资源
        TransactionUtil.close(sqlSession);
    }

    @Override
    public String save(Question question, boolean flag) {
        SqlSession sqlSession = null;
        try{
            sqlSession = MapperFactory.getSqlSession();
            //获取映射对象
            QuestionMapper mapper = MapperFactory.getMapper(sqlSession, QuestionMapper.class);
            //获取uuid并设置
            String id = UUID.randomUUID().toString().replace("-", "");
            question.setId(id);
            //设置审核属性 未审核
            question.setReviewStatus("0");
            //设置创建日期
            question.setCreateTime(new Date());
            //如果上传了图片 设置图片属性
            if(flag){
                question.setPicture(id);
            }
            //保存
            mapper.save(question);
            //提交事务
            TransactionUtil.commit(sqlSession);
            return id;
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        }finally {
            try{
                TransactionUtil.close(sqlSession);
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }
}
